
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

enum Dpositon { top, bottom, center }

class Toast {
  static OverlayEntry? _overlayEntry;

  static bool _isVisible = false;

  static Future<void> show(BuildContext context,
      {required message,
      String status = '1',
      double? top,
      Dpositon position = Dpositon.center,
      Duration duration = const Duration(milliseconds: 3000)}) async {
    if (_isVisible) {
      return;
    }

    double widgetTop;

    if (top != null) {
      widgetTop = top;
    } else {
      if (position == Dpositon.center) {
        widgetTop = (MediaQuery.of(context).size.height * 0.5);
      } else if (position == Dpositon.bottom) {
        widgetTop = (MediaQuery.of(context).size.height * 0.8);
      } else {
        widgetTop = (MediaQuery.of(context).size.height * 0.2);
      }
    }

    _overlayEntry = OverlayEntry(
      builder: (context) => Positioned(
        top: widgetTop,
        width: MediaQuery.of(context).size.width,
        child: Material(
          color: Colors.transparent,
          child: ToastContent(
            message: message,
            status: status,
          ),
        ),
      ),
    );

    Overlay.of(context).insert(_overlayEntry!);

    _isVisible = true;

    await Future.delayed(duration, hide);
  }

  static void hide() {
    if (_overlayEntry != null) {
      _overlayEntry!.remove();

      _overlayEntry = null;
    }

    _isVisible = false;
  }
}

class ToastContent extends StatefulWidget {
  final String message;

  final String status;

  ToastContent({required this.message, required this.status});

  @override
  _ToastContent createState() => _ToastContent();
}

class _ToastContent extends State<ToastContent> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          Expanded(child: SizedBox()),
          Container(
              padding: EdgeInsets.symmetric(horizontal: 10.0.w, vertical: 10.0.w),
              decoration: BoxDecoration(
                color: AppColors.c_33333370,
                borderRadius: BorderRadius.circular(10.w),
                boxShadow: [
                  BoxShadow(
                    color: Color.fromRGBO(157, 157, 157, 0.18),
                    offset: Offset(0, 4),
                    blurRadius: 12.w,
                    spreadRadius: 0,
                  ),
                ],
              ),
              child: ConstrainedBox(
                constraints: BoxConstraints(
                  maxWidth: 300.w, // 设置最大宽度为200
                ),
                child: Stack(
                  children: [
                    Positioned(
                      top: 0,
                      left: 0,
                      bottom: 0,
                      child: Container(
                        alignment: Alignment.center,
                        child: widget.status == '1'
                            ? Image.asset(
                                'assets/images/dapp/success.png',
                                width: 20.w,
                                fit: BoxFit.fitWidth,
                              )
                            : Image.asset(
                                'assets/images/dapp/error.png',
                                width: 20.w,
                                fit: BoxFit.fitWidth,
                              ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 24.w),
                      child: Text(
                        widget.message,
                        style: TextStyle(
                          decoration: TextDecoration.none,
                          fontWeight: FontWeight.w400,
                          color: AppColors.c_white,
                          fontSize: 14.0.sp,
                        ),
                      ),
                    ),
                  ],
                ),
              )),
          Expanded(child: SizedBox()),
        ],
      ),
    );
  }
}
